//
//  LXMineTableViewController.m
//  cat
//
//  Created by imera on 16/1/16.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "LXMineTableViewController.h"
#import "LXLoginView.h"
#import "LXUserInfoCellView.h"
#import "LXMineCellView.h"
#import "UIImage+Lx.h"
#import "LXLoginViewController.h"
#import "LXUserInfoViewController.h"
#import "LXUserLoginCell.h"
#import "AFNetworking.h"
#import "LXMarkViewController.h"
#import "MBProgressHUD+MJ.h"
#import "LXDownloadViewController.h"
#import "LXHistoryViewController.h"
#import "LXConcernListViewController.h"
#import "LXAboutUsViewController.h"

@interface LXMineTableViewController () <LXUserLoginCellDelegate>

@property (nonatomic, weak) LXUserInfoCellView *userInfo;

@property (nonatomic, weak) LXUserLoginCell *userLoginInfo;

@end

@implementation LXMineTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = MainBackgroundColor;
    
    [self initSubViews];
    
    [[NSUserDefaults standardUserDefaults]addObserver:self forKeyPath:@"password" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}

#pragma mark - kvo 监听用户是否注销
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"password"]) {
         [self initSubViews];
    }
}

#pragma mark 初始化子控件
- (void)initSubViews
{
    /**  用户登录 */
    LXUserInfoCellView *userInfo = [[LXUserInfoCellView alloc]init];
    UITapGestureRecognizer *userTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(userTap)];
    [userInfo addGestureRecognizer:userTap];
    
    CGFloat userInfoX = 0;
    CGFloat userInfoY = 10;
    CGFloat userInfoW = self.view.frame.size.width;
    CGFloat userInfoH = 80;
    userInfo.frame = CGRectMake(userInfoX, userInfoY, userInfoW, userInfoH);
    self.userInfo = userInfo;
    [self.view addSubview:userInfo];
    
    
    /**  用户未登录 */
    LXUserLoginCell *userLoginInfo = [[LXUserLoginCell alloc]init];
    userLoginInfo.MyDelegate = self;
    CGFloat userLoginInfoX = 0;
    CGFloat userLoginInfoY = 10;
    CGFloat userLoginInfoW = self.view.frame.size.width;
    CGFloat userLoginInfoH = 80;
    userLoginInfo.frame = CGRectMake(userLoginInfoX, userLoginInfoY, userLoginInfoW, userLoginInfoH);
    self.userLoginInfo = userLoginInfo;
    [self.view addSubview:userLoginInfo];
    
    
    CGRect downFrame = [self addCell:@"离线缓存" image:@"marquee-download" marginY:CGRectGetMaxY(userInfo.frame) + 10 tag:0];
    CGRect collectFrame = [self addCell:@"收藏" image:@"star-2" marginY:CGRectGetMaxY(downFrame) + 5 tag:1];
    CGRect clockFrame = [self addCell:@"历史记录" image:@"revert" marginY:CGRectGetMaxY(collectFrame) + 5 tag:2];
    CGRect attentionFrame = [self addCell:@"关注的人" image:@"users" marginY:CGRectGetMaxY(clockFrame) + 5 tag:3];
    [self addCell:@"关于我们 " image:@"notes" marginY:CGRectGetMaxY(attentionFrame) + 5 tag:4];
    
    /** 判断用户是否登录 */
    NSString *userName = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
    if (userName == nil) { // 未登录
        userLoginInfo.hidden = NO;
        userInfo.hidden = YES;
        self.user = nil;
    }else{
        userLoginInfo.hidden = YES;
        userInfo.hidden = NO;
        [self getUserInfo:userInfo];
    }
}

#pragma mark - 获取用户数据
- (void)getUserInfo:(LXUserInfoCellView *)userInfo
{
    NSString *userName = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
    NSString *password = [[NSUserDefaults standardUserDefaults]objectForKey:@"password"];
    if (![userName isEqualToString:@""] && ![password isEqualToString:@""]) {
        AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
        NSString *url = @"http://cat666.com/cat666-interface/index.php/index/check";
        [mgr POST:url parameters:@{@"user":userName,@"password":password} success:^(AFHTTPRequestOperation *operation, id response) {
            NSDictionary *dict = response;
            if (dict.allKeys.count == 1) {
                
            }else{
                LXUser *user = [LXUser initWithDict:dict];
                self.user = user;
                userInfo.user = user;
            }
        } failure:^(AFHTTPRequestOperation * operation, NSError * error) {
            self.userLoginInfo.hidden = NO;
            self.userInfo.hidden = YES;
        }];
    }
    
}

#pragma mark - 用户信息被点击
- (void)userTap
{
    if ([self.myDelegate respondsToSelector:@selector(pushLoginViewController:pushVC:)]) {
        LXUserInfoViewController *userInfoViewController = [[LXUserInfoViewController alloc]init];
        userInfoViewController.user = self.user;
        [self.myDelegate pushLoginViewController:self pushVC:userInfoViewController];
    }
}

- (CGRect)addCell:(NSString *)title image:(NSString *)image marginY:(CGFloat)marginY tag:(int)tag
{
    LXMineCellView *cell = [[LXMineCellView alloc]initWithTitle:title image:image];
    cell.tag = tag;
    UITapGestureRecognizer *cellTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cellTap:)];
    [cell addGestureRecognizer:cellTap];
    
    CGFloat cellX = 0;
    CGFloat cellY = marginY;
    CGFloat cellW = self.view.frame.size.width;
    CGFloat cellH = 40;
    cell.frame = CGRectMake(cellX, cellY, cellW, cellH);
    [self.view addSubview:cell];
    return cell.frame;
}

#pragma mark - 栏目被点击
- (void)cellTap:(UIGestureRecognizer *)tap
{
    switch (tap.view.tag) {
        case 0:
            [self pushDownloadVc];
            break;
        case 1:
            [self pushMarkVc];
            break;
        case 2:
            [self pushHistoryVc];
            break;
        case 3:
            [self pushConcernVc];
            break;
        case 4:
            [self pushAboutUsVc];
            break;
        default:
            break;
    }
}

#pragma mark - 离线缓存控制器
- (void)pushDownloadVc
{
    LXDownloadViewController *downloadVc = [[LXDownloadViewController alloc]init];
    [self.navigationController pushViewController:downloadVc animated:YES];
}

#pragma mark - 收藏控制器
- (void)pushMarkVc
{
    if (self.user != nil) {
        LXMarkViewController *markVc = [[LXMarkViewController alloc]init];
        markVc.user = self.user;
        [self.navigationController pushViewController:markVc animated:YES];
    }else{
        [MBProgressHUD showError:@"请先登录"];
        LXLoginViewController *loginVC = [[LXLoginViewController alloc]init];
        [self.navigationController pushViewController:loginVC animated:YES];
    }

}

#pragma mark - 历史记录
- (void)pushHistoryVc
{
    LXHistoryViewController *historyVc = [[LXHistoryViewController alloc]init];
    [self.navigationController pushViewController:historyVc animated:YES];
}

#pragma mark - 关注的人
- (void)pushConcernVc
{
    if (self.user != nil) {
        LXConcernListViewController *concernVc = [[LXConcernListViewController alloc]init];
        concernVc.user = self.user;
        [self.navigationController pushViewController:concernVc animated:YES];
    }else{
        [MBProgressHUD showError:@"请先登录"];
        LXLoginViewController *loginVC = [[LXLoginViewController alloc]init];
        [self.navigationController pushViewController:loginVC animated:YES];
    }
}

#pragma mark - 关于我们
- (void)pushAboutUsVc
{
    LXAboutUsViewController *aboutUsVc = [[LXAboutUsViewController alloc]init];
    [self.navigationController pushViewController:aboutUsVc animated:YES];
}

#pragma mark - 登录按钮被点击
- (void)login
{
    if ([self.myDelegate respondsToSelector:@selector(pushLoginViewController:pushVC:)]) {
        LXLoginViewController *loginVc = [[LXLoginViewController alloc]init];
        [self.myDelegate pushLoginViewController:self pushVC:loginVc];
    }
}

#pragma mark - 登录cell代理方法
- (void)login:(LXUserLoginCell *)loginCell
{
    LXLoginViewController *loginVc = [[LXLoginViewController alloc]init];
    [self.navigationController pushViewController:loginVc animated:YES];
}

- (void)dealloc
{
    [[NSUserDefaults standardUserDefaults]removeObserver:self forKeyPath:@"password"];
    
}

@end
